package com.diary.server.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.diary.common.context.BaseContext;
import com.diary.server.entity.DiaryList;
import com.diary.server.mapper.DiaryListMapper;
import com.diary.server.service.DiaryService;
import com.diary.server.vo.CalendarVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DiaryServiceImpl extends ServiceImpl<DiaryListMapper, DiaryList> implements DiaryService {

    @Autowired
    private DiaryListMapper diaryListMapper;


    public List<DiaryList> getByDate(String dateStr, Long userId) {
        LocalDate date = LocalDate.parse(dateStr);
        LocalDateTime startOfDay = date.atStartOfDay();
        LocalDateTime endOfDay = date.atTime(23, 59, 59);

        return this.list(
                new LambdaQueryWrapper<DiaryList>()
                        .eq(DiaryList::getUserId, userId)
                        .ge(DiaryList::getRecordTime, startOfDay)
                        .le(DiaryList::getRecordTime, endOfDay)
                        .orderByDesc(DiaryList::getRecordTime)
        );
    }

    public List<CalendarVO> getByMonth(String month) {
        Long userId = BaseContext.getCurrentId();
        List<CalendarVO> calendarVOList = new ArrayList<CalendarVO>();
        log.info("userId: {}", userId,month);
        if (userId != null) {
            calendarVOList = diaryListMapper.getByMonth(month, userId);

        }
        return calendarVOList;
    }

    public List<DiaryList> getWeeklyDiaries(Long userId, LocalDate startDate, LocalDate endDate) {
        return list(new QueryWrapper<DiaryList>()
                .eq("user_id", userId)
                .ge("record_time", startDate.atStartOfDay())
                .le("record_time", endDate.atTime(23, 59, 59))
                .orderByAsc("record_time"));
    }

}
